<template>
	<div style="text-align: center;">
		<h2>你好！{{ userInfo.username }} </h2>
		<el-form :model="passForm" status-icon :rules="rules" ref="passForm" label-width="100px">
			<el-form-item label="旧密码" prop="oldPassword">
				<el-input type="password" v-model="passForm.oldPassword" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="新密码" prop="newPassword">
				<el-input type="password" v-model="passForm.newPassword" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="checkPass">
				<el-input type="password" v-model="passForm.checkPass" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="submitForm('passForm')">提交</el-button>
				<el-button @click="resetForm('passForm')">重置</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>

	export default {
		name: "Login",
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.passForm.newPassword) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				userInfo: {
				},
				passForm: {
          newPassword: '',
					checkPass: '',
          oldPassword: ''
				},
				rules: {
          newPassword: [
						{ required: true, message: '请输入新密码', trigger: 'blur' },
						{ min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
					],
					checkPass: [
						{ required: true, validator: validatePass, trigger: 'blur' }
					],
          oldPassword: [
						{ required: true, message: '请输入当前密码', trigger: 'blur' },
					]
				}
			}
		},
		created() {
			this.getUserInfo()
		},
		methods: {
			getUserInfo() {
				this.$axios.get("/nb/user/getUserInfo").then(res => {
					this.userInfo = res.data.data;
				})
			},
      logout(){
        let flag=this;
        this.$axios.post("/logout").then(function (result) {
          localStorage.clear();
          sessionStorage.clear();
          flag.$store.commit("resetState");
          flag.$router.push("/login");
        })
      },
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
            this.$confirm('此操作将会让您重新登录，未保存的工作将会失效, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$axios.post('/nb/user/changPassword',this.passForm).then(res=>{
                if (res.data.data){
                  this.$message.success("修改成功，即将跳转到登录页面！")
                  this.logout();
                }else {
                  this.$message.error('修改失败，旧密码错误!');
                }
              })
            })
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style scoped>
.el-form {
	width: 420px;
	margin: 50px auto;
}
</style>